import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'root',
      component: () => import('../views/HomeView.vue')
    },
    {
      path: '/userlogin',
      name: 'userlogin',
      component: () => import('../views/user/LoginView.vue')
    },
    {
      path: '/userregister',
      name: 'userregister',
      component: () => import('../views/user/RegisterView.vue')
    },
    {
      path: '/user',
      name: 'user',
      redirect: '/usercenter',
      component: () => import('../components/user/Layout.vue'),
      children: [
        {
          path: '/usercenter',
          name: 'usercenter',
          component: () => import('../views/user/UsercenterView.vue'),
          meta: { requiresAuth: true }
        },
        {
          // 动态路由参数
          path: '/productdetail/:productId',
          name: 'productdetail',
          component: () => import('../components/ProductDetail.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/order',
          name: 'userorder',
          component: () => import('../views/user/OrderView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/address',
          name: 'address',
          component: () => import('../views/user/AddressView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/wish',
          name: 'wish',
          component: () => import('../views/user/WishView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/collect',
          name: 'collect',
          component: () => import('../views/user/CollectionView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/score',
          name: 'score',
          component: () => import('../views/user/ScoreView.vue'),
          meta: { requiresAuth: true }
        },
      ]
    },
    {
      path: '/adminlogin',
      name: 'adminlogin',
      component: () => import('../views/admin/LoginView.vue')
    },
    {
      path: '/admin',
      name: 'admin',
      redirect: '/admin/overview',
      component: () => import('../components/admin/Layout.vue'),
      children: [
        {
          path: '/admin/overview',
          name: 'overview',
          component: () => import('../views/admin/OverviewView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/admin/manage',
          name: 'manage',
          component: () => import('../views/admin/IndexView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/admin/category',
          name: 'category',
          component: () => import('../views/admin/CategoryView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/admin/order',
          name: 'order',
          component: () => import('../views/admin/OrderView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/admin/thing',
          name: 'thing',
          component: () => import('../views/admin/ThingView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/admin/userbehavior',
          name: 'userbehavior',
          component: () => import('../views/admin/UserBehaviorView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/admin/op-log',
          name: 'op-log',
          component: () => import('../views/admin/OpLogViewm.vue'),
          meta: { requiresAuth: true }
        },
      ]
    },
    {
      path: '/test',
      name: 'test',
      component: () => import('../components/echars/Test.vue'),
      // meta: { requiresAuth: true }
    },
  ]
})


// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 是否授权
  let isAuthenticated = false;
  if (sessionStorage.getItem("Authentication") != null) {
    isAuthenticated = true

  }
  if (to.matched.some(record => record.meta.requiresAuth) && !isAuthenticated) {
    // 如果目标路由需要身份验证且用户未登录，则重定向到登录页面
    next({ path: '/' })
  } else {
    // 否则，继续导航
    next()
  }
})

export default router
